import numpy as np
from scipy.special import gamma


# 图像分块
# 分成num个size*size大小的图像
def div_pic(pic, size, num):
    ret = []
    for row in range(num):
        temp = []
        for col in range(num):
            ar = np.zeros((size, size))
            for i in range(size):
                for j in range(size):
                    x = i + row * size
                    y = j + col * size
                    ar[i][j] = pic[x][y]
            temp.append(ar)
        ret.append(temp)
    return ret

# 要求解的函数
def f(x, m1, m2):
    return (gamma(2 / x) * gamma(2 / x)) / (gamma(1 / x) * gamma(3 / x)) - (m1 * m1) / m2


# 牛顿法
def newton_slove(x, m1, m2):
    det = 0.0000005
    eps = 0.0000001

    for k in range(100):
        df = (f(x + det, m1, m2) - f(x, m1, m2)) / (2 * det)
        tx = x - (f(x, m1, m2) / df)
        if abs(tx - x) < eps:
            x = tx
            break
        x = tx
    return x

